import http from '@/util/http'
import { message } from '@/util/message'
// 获取接口域名
export function getDomain() {
    return localStorage.getItem('serverHost') || 'http://localhost';
}
// 修改接口域名
export function setDomain(domain) {
    return localStorage.setItem('serverHost', domain);
}
// 获取机器人数据
export function getBotData() {
    return http.get('/index/init');
}

// 日期转化
export function timestampToDate(timestamp) {

    return timestamp == 0 ? '-' : new Date(timestamp * 1000).toLocaleString();
}
// 复制
export function copyText(text) {

    let input = document.createElement("input");
    document.body.appendChild(input);
    input.value = text;
    input.focus();
    input.select();
    try {
        let result = document.execCommand("copy");
        document.body.removeChild(input);
        if (!result) {
            message("error","复制失败");
        } else {
            message("success","复制成功");
        }
    } catch (e) {
        document.body.removeChild(input);
        message("error","当前浏览器不支持复制功能，请检查更新或更换其他浏览器操作");
    }
}

// tag type
export function tagType(type)
{
    let arr = ['primary', 'success', 'info', 'warning', 'danger'];
    return arr.includes(type) ? type : arr[Math.floor(Math.random() * 5)];
}

// 图片上传检测
export function beforeImageUpload(rawFile)
{
    if (!rawFile.type.startsWith('image/')) {
        message('error', '图片格式有误')
        return false
    } else if (rawFile.size / 1024 / 1024 > 10) {
        message('error', '图片大小不能超过 10MB!')
        return false
    }
    return true
}

// 视频上传检测
export function beforeVideoUpload(rawFile)
{
    if (!rawFile.type.startsWith('video/')) {
        message('error', '视频格式有误')
        return false
    } else if (rawFile.size / 1024 / 1024 > 50) {
        message('error', '视频大小不能超过 10MB!')
        return false
    }
    return true
}

// 音频上传检测
export function beforeAudioUpload(rawFile)
{
    if (!rawFile.type.startsWith('audio/')) {
        message('error', '音频格式有误')
        return false
    } else if (rawFile.size / 1024 / 1024 > 20) {
        message('error', '音频大小不能超过 10MB!')
        return false
    }
    return true
}

// 文件上传检测
export function beforeFileUpload(rawFile)
{

    let allowedTypes = {
        'text/plain': ['txt'],
        'application/pdf': ['pdf'],
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['docx'],
        'application/vnd.ms-word': ['doc'],
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['xlsx'],
        'application/vnd.ms-excel': ['xls'],
        'application/vnd.openxmlformats-officedocument.presentationml.presentation': ['pptx'],
        'application/vnd.ms-powerpoint': ['ppt'],
        'application/zip': ['zip'],
        'application/rar': ['rar'],
        'application/x-zip-compressed': ['zip'],
        'application/x-rar-compressed': ['rar'], // 这个MIME类型可能不准确，具体取决于浏览器和RAR文件的版本
    };

    let success = false;

    for (let mimeType in allowedTypes) {
        // console.log(mimeType,rawFile.type.startsWith(mimeType))
        if (rawFile.type.startsWith(mimeType)) {
            success = true;
            break;
        }
    }

    if (!success) {
        console.log(rawFile.type)
        message('error', '文件格式有误')
        return false
    } else if (rawFile.size / 1024 / 1024 > 100) {
        message('error', '文件大小不能超过 100MB!')
        return false
    }
    return true
}
